package com.wecommodule.bpm.ext.biz.service.impl;

import com.wecommodule.bpm.ext.biz.entity.ParameterListVo;
import com.wecommodule.bpm.ext.biz.entity.XingZuoVo;
import com.wecommodule.bpm.ext.biz.service.DailyPushUsersService;
import com.wecommodule.bpm.ext.biz.service.SendMessageService;
import com.wecommodule.bpm.ext.biz.util.DateCalculation;
import com.wecommodule.bpm.ext.biz.util.TianApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class DailyPushUsersImpl implements DailyPushUsersService {


    @Value("${tianApi.caiApiKey}")
    private String caiApiKey;

    @Value("${tianApi.tianApiKey}")
    private String tianApiKey;


    @Value("${tianApi.XingZuoVlaue}")
    private String XingZuoVlaue;


    @Value("${tianApi.WeatherVlaue}")
    private String WeatherVlaue;



    @Value("${weixin.userIds}")
    private String userIds;

    @Value("${target.birthday}")
    private String birthday;


    @Value("${target.romanticDate}")
    private String romanticDate;

    @Resource
    private SendMessageService sendMessage;

    @Override
    public void sendTextcardMsg(){
        ParameterListVo parameterList = getParameterList();
        String title = "";


        StringBuffer message = new StringBuffer();
        if(parameterList.getCountBirthday() == 0){
            message.append("\uD83C\uDF82建发测试！\n");
        }
        if (parameterList.getWeatherVo() != null){
            //设置标题
            title = "\uD83C\uDF08祝你生日快乐~        " +parameterList.getWeatherVo().getDate() + " " + parameterList.getWeatherVo().getWeek();

            //消息内容，微信显示不了太多，不要弄太多了
            message.append("\uD83C\uDFF0生日信息1：").append(parameterList.getWeatherVo().getArea());
            message.append("\n\uD83C\uDF25生日信息1：").append(parameterList.getWeatherVo().getWeather());
            message.append("\n\uD83C\uDF21生日信息1：").append(parameterList.getWeatherVo().getReal());
            message.append("\n\uD83E\uDD75生日信息1：").append(parameterList.getWeatherVo().getHighest());
            message.append("\n\uD83E\uDD76生日信息1：").append(parameterList.getWeatherVo().getLowest());
            message.append("\n生日信息1：").append(parameterList.getWeatherVo().getPop()).append("%");
            message.append("\n\uD83D\uDCA8生日信息1：").append(parameterList.getWeatherVo().getWindsc());

            message.append("\n\uD83C\uDFD6生日信息1：").append(parameterList.getWeatherVo().getTips());
        }


        message.append("\n\uD83C\uDF82距离你下个生日还有").append(parameterList.getCountBirthday()).append("11");

        if (parameterList.getCountRomanticDate() == 0){
            //判断是否当天生日
            message.append("\n\uD83D\uDC91你今天生日了~");
        }else{
            message.append("\n\uD83D\uDC91离你的生日还有多少天").append(parameterList.getCountRomanticDate()).append("天噢~");
        }

        if (StringUtils.isNotEmpty(parameterList.getCaiHongPi())){
            message.append("\n\n\uD83D\uDC8C").append(parameterList.getCaiHongPi());
        }

        //设置卡片跳转链接
        String url = "http://www.baidu.com?# 《企业微信生日推送操作流程》";

        //循环推送多个用户
        sendMessage.sendTextcardMsg(userIds,title,message.toString(),url);
    }

    @Override
    public void sendTextMsg() {
        String message = "测试消息";
        sendMessage.sendTextMsg(userIds.toString(),message);
    }

    @Override
    public void sendNewsMsg() {
        //设置卡片跳转链接
//        String url = "https://www.yuque.com/docs/share/00269daa-1dcb-402a-bb2a-9f838984443d?# 《企业微信每日早安推送操作流程》";
        String url = "http://www.baidu.com?# 《每日垃圾话》";
		//图片地址
        String imgUrl = "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png";
        sendMessage.sendNewsMsg(userIds, "他奶奶滴，你喝水啊！为什么不喝水", "水是如此的珍贵！让同志们先喝！", url, imgUrl);
    }



    private ParameterListVo getParameterList(){
        ParameterListVo parameterList = new ParameterListVo();
        System.out.println("打印发送请求的组装参数： " +parameterList);
        if (StringUtils.isNotEmpty(tianApiKey) && StringUtils.isNotEmpty(WeatherVlaue)){

            parameterList.setWeatherVo(TianApi.getWeather(tianApiKey, WeatherVlaue));
        }

            if (StringUtils.isNotEmpty(caiApiKey)){

                parameterList.setCaiHongPi(TianApi.getCaiHongPi(caiApiKey));

            parameterList.setSaylove(TianApi.getSaylove(tianApiKey));
        }


        if (StringUtils.isNotEmpty(tianApiKey) && StringUtils.isNotEmpty(XingZuoVlaue)){

            XingZuoVo xingZuo = TianApi.getXingZuo(tianApiKey, XingZuoVlaue);
            System.out.println(xingZuo);
            parameterList.setXingZuo(TianApi.getXingZuo(tianApiKey, XingZuoVlaue));
            log.info(parameterList.getXingZuo().getTitle());
        }


        if (StringUtils.isNotEmpty(birthday)){

            parameterList.setCountBirthday(DateCalculation.CountBirthday(birthday));
        }


        if (StringUtils.isNotEmpty(romanticDate)){

            parameterList.setCountRomanticDate(DateCalculation.getRomanticDate(romanticDate));
        }

        return parameterList;
    }

}
